﻿using System;
using System.Runtime.Serialization;
using Peak.Core;

namespace Peak.Web.Controllers.Models
{
  public class StoryFilterModel 
  {
    public int Page { get; set; }
    
    public int? PageSize { get; set; }

    private bool _statusToDo;
    public bool StatusToDo
    {
      get
      {
        return _statusToDo;
      }
      set
      { 
        if(_statusToDo != value)
        {
          Page = 0;
          _statusToDo = value;
        }
      }
    }

    private bool _statusInProgress;
    public bool StatusInProgress
    {
      get
      {
        return _statusInProgress;
      }
      set
      {
        if (_statusInProgress != value)
        {
          Page = 0;
          _statusInProgress = value;
        }
      }
    }

    private bool _statusVerify;
    public bool StatusVerify
    {
      get
      {
        return _statusVerify;
      }
      set
      {
        if (_statusVerify != value)
        {
          Page = 0;
          _statusVerify = value;
        }
      }
    }

    private bool _statusDone;
    public bool StatusDone
    {
      get
      {
        return _statusDone;
      }
      set
      {
        if (_statusDone != value)
        {
          Page = 0;
          _statusDone = value;
        }
      }
    }

    private string _quickFilter;
    public string QuickFilter
    {
      get
      {
        return _quickFilter;
      }
      set
      {
        if (_quickFilter != value)
        {
          Page = 0;
          _quickFilter = value;
        }
      }
    }
    public SortDirection? SortDirection { get; set; }
    public bool? AssignedToSprint { get; set; }

    private StorySortOrder? _sortOrder;
    public StorySortOrder? SortOrder
    {
      get
      {
        return _sortOrder;
      }
      set
      {
        if(_sortOrder != value)
        {
          _sortOrder = value;
          if(_sortOrder == StorySortOrder.Priority)
            SortDirection = Core.SortDirection.Desc;          
          Page = 0;
        }
      }
    }    

    public SortDirection InverseSortDirection(StorySortOrder sortOrder)
    {
      if(SortOrder != sortOrder)
        return Core.SortDirection.Asc;
      if (SortDirection == null || SortDirection == Core.SortDirection.Asc)
        return Core.SortDirection.Desc;
      else
        return Core.SortDirection.Asc;
    }

    public StoryFilterModel()
    {
      Page = 0;
      PageSize = null;
      SortOrder = StorySortOrder.Priority;
      SortDirection = Core.SortDirection.Desc;
      StatusInProgress = StatusToDo = StatusVerify = true;
      StatusDone = false;
    }

    public void Update(StoryFilter storyFilter, int projectId)
    {      
      storyFilter.ProjectId = projectId;
      storyFilter.Page = Page;
      storyFilter.QuickFilter = QuickFilter;
      storyFilter.PageSize = PageSize;
      if (SortDirection.HasValue)
        storyFilter.SortDirection = SortDirection.Value;
      if (SortOrder.HasValue)
        storyFilter.SortOrder = SortOrder.Value;
      if (StatusToDo)
        storyFilter.Status |= FilterStoryStatus.ToDo;
      if (StatusInProgress)
        storyFilter.Status |= FilterStoryStatus.InProgress;
      if (StatusVerify)
        storyFilter.Status |= FilterStoryStatus.Verify;
      if (StatusDone)
        storyFilter.Status |= FilterStoryStatus.Done;
      if (AssignedToSprint.HasValue && AssignedToSprint.Value)
        storyFilter.AssignedToSprint = AssignedToSprint.Value;      
    }

    public string GetDescription()
    {
      string result = "";
      if(StatusToDo && StatusInProgress && StatusVerify && StatusDone)
      {
        result = "All stories";
      }
      else
      {
        result = "stories in status [";
        if(StatusToDo)
        {
          result += "To do,";
        }
        if(StatusInProgress)
        {
          result += "in progress,";
        }
        if(StatusVerify)
        {
          result += "verify,";
        }
        if(StatusDone)
        {
          result += "done,";
        }
        result = result.Remove(result.Length - 1, 1);
        result += "]";      
      }
      return result;
    }

    public StoryFilterModel GetFilter(StorySortOrder order, SortDirection direction)
    {
      StoryFilterModel storyFilterModel = new StoryFilterModel();
      storyFilterModel.AssignedToSprint = AssignedToSprint;
      storyFilterModel.Page = Page;
      storyFilterModel.PageSize = PageSize;
      storyFilterModel.QuickFilter = QuickFilter;
      storyFilterModel.SortOrder = order;
      storyFilterModel.SortDirection = direction;
      storyFilterModel.StatusDone = StatusDone;
      storyFilterModel.StatusInProgress = StatusInProgress;
      storyFilterModel.StatusToDo = StatusToDo;
      storyFilterModel.StatusVerify = StatusVerify;
      return storyFilterModel;
    }
  }
}